const app = getApp()
var WxParse = require('../../wxParse/html2json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    cardId:'',
    levle:'',
    is_show_sort: false,
    product: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  closeSort: function() {
    this.setData({
      is_show_sort: false
    })
  },
  openSort: function() {
    this.setData({
      is_show_sort: true
    })
  },
  onLoad: function(options) {
    console.log("00000000---",options)
    let that = this;
    let { code, productName, searchType ,levle } = options

    if (code =="card_nianka"){
      that.setData({
        code: code,
      })
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.URL + '/app/memberCard/cardClassify?openid=' + openid + '&levle=' + levle,
            success: function (res) {
              console.log(res)
              wx.hideLoading()
              that.setData({
                cardId: res.data.card[0].id,
                levle: res.data.card[0].levle,
                price: res.data.card[0].price,
                levleNotes: res.data.card[0].levleNotes
              })

              console.log("cardId:", that.data.cardId)
              console.log("levle:", that.data.levle)
            }
          })
        },
      })
      
    }else{
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data

          wx.request({
            url: app.globalData.URL + '/app/product/proClass?code=' + code + '&productName=' + productName + '&searchType=' + searchType + '&openid='+openid,
            success: function (res) {
              console.log(res.data)

              that.setData({
                list: res.data.list || [],
                product: res.data.product || [],
                paseImg: that.parseImg(res.data.product, that)
              })
            }
          })
        },
      })
      
    }
    

  },


  parseImgCard: function (data, that) {

    var parseImgObj = {}
    for (var i = 0; i < data.length; i++) {
      parseImgObj[data[i].id] = WxParse.html2json(data[i]['url'], 'returnData');
    }
    return parseImgObj
  },

  parseImg: function (data, that) {

    var parseImgObj = {}
    for (var i = 0; i < data.length; i++) {
      parseImgObj[data[i].id] = WxParse.html2json(data[i]['productClassPic'], 'returnData');
    }
    return parseImgObj    
  },

  toShoppingCart:function(){
    wx.navigateTo({
      url: '../shoppingCart/shoppingCart',
    })
  },
  joinCar:function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/product/addShopping?proId=' + id + '&num=1' + '&openid=' + openid,
          success: function (res) {
            wx.hideLoading()
            console.log(res)
            if (res.data.msg == true) {
              wx.showToast({
                title: '加入成功',
                icon: 'success',
              })
            } else {
              wx.showToast({
                title: '加入购物车失败',
                icon: '../../images/error.png',
              })
            }
          }
        })
      },
    })
  }
})